import Vue from 'vue'
import { addRouter } from '@/router'
import router from '@/router'
import command from '@/utils/command'
Vue.use(command)

const state = {
  storeOpts: [],
  vendorOpts: [],
}
const mutations = {
  SET_STORE: (state, data) => {
    state.storeOpts = data
  },
  SET_VENDOR: (state, data) => {
    state.vendorOpts = data
  },
}

const actions = {
  getStoreList({ commit, dispatch }, parameter = {}) {
    return new Promise((resolve) => {
      command
        .executeCommand({
          api: '/admin-api/store/store/list',
          parameter,
        })
        .then((result) => {
          let data = result.data.map((e) => {
            return {
              key: e.storeCode,
              value: e.storeName,
            }
          })
          commit('SET_STORE', data)
          resolve(state.storeOpts)
        })
    })
  },
  getVendorList({ commit, dispatch }, parameter = {}) {
    return new Promise((resolve) => {
      command
        .executeCommand({
          api: '/admin-api/store/vendor/list',
          parameter,
        })
        .then((result) => {
          let data = result.data.map((e) => {
            return {
              key: e.vendorCode,
              value: e.name,
            }
          })
          commit('SET_VENDOR', data)
          resolve(state.vendorOpts)
        })
    })
  },
}

export default {
  namespaced: true,
  mutations,
  state,
  actions,
}
